import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";
import _regeneratorRuntime from "@babel/runtime/regenerator";

/**
 * @model 卡片管理
 * @modelState {object} [pagination=false] - 分页信息
 * @modelState {object[]} [dataSource=[]] - 数据
 * @modelState {object[]} [fdLevel=[]] - 层级的值集
 * @modelState {object[]} [catalogType=[]] - 分类的值集
 * @modelState {object} queryPagination - 上一次查询成功的分页信息
 *
 * @modelEffect cardFetch - 查询分页的卡片信息
 * @modelEffect cardCreate - 新建卡片
 * @modelEffect cardUpdate - 更新卡片
 * @modelEffect cardDetails - 获取卡片详情
 * @modelEffect cardTenantFetch - 查询分页的卡片分配的租户
 * @modelEffect cardTenantAdd - 给卡片新增租户
 * @modelEffect cardTenantRemove - 删除卡片已经分配的租户
 *
 * @modelReducer updateState - 更新 model
 *
 * @date 2019-01-24
 */
import { createPagination, getResponse } from 'utils/utils';
import { queryMapIdpValue } from 'cpaas-front/lib/services/api';
import { cardInsert, cardQueryPage, cardTenantDelete, cardTenantInsert, cardTenantQueryPage, cardUpdate as _cardUpdate, cardDetails as _cardDetails } from "../services/cardManageService";
export default {
  namespace: 'cardManage',
  state: {
    // 卡片管理界面数据
    pagination: false,
    dataSource: [],
    fdLevel: [],
    // 层级的值集
    catalogType: [] // 分类的值集
    // queryPagination: undefined, // 存储的上一次 查询成功的分页信息
    // 分配卡片模态框数据
    // cardTenantDataSource: [], // 卡片租户的数据
    // cardTenantPagination: false, // 卡片租户的分页

  },
  effects: {
    init: /*#__PURE__*/_regeneratorRuntime.mark(function init(_, _ref) {
      var call, put, res, realRes;
      return _regeneratorRuntime.wrap(function init$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              call = _ref.call, put = _ref.put;
              _context.next = 3;
              return call(queryMapIdpValue, {
                fdLevel: 'CPFM.DATA_TENANT_LEVEL',
                catalogType: 'CPFM.DASHBOARD_CARD.TYPE'
              });

            case 3:
              res = _context.sent;
              realRes = getResponse(res);

              if (!realRes) {
                _context.next = 8;
                break;
              }

              _context.next = 8;
              return put({
                type: 'updateState',
                payload: {
                  fdLevel: realRes.fdLevel,
                  catalogType: realRes.catalogType
                }
              });

            case 8:
            case "end":
              return _context.stop();
          }
        }
      }, init);
    }),
    // 查询卡片
    cardFetch: /*#__PURE__*/_regeneratorRuntime.mark(function cardFetch(_ref2, _ref3) {
      var payload, call, put, res, realRes, page, sort;
      return _regeneratorRuntime.wrap(function cardFetch$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref2.payload;
              call = _ref3.call, put = _ref3.put;
              _context2.next = 4;
              return call(cardQueryPage, payload);

            case 4:
              res = _context2.sent;
              realRes = getResponse(res);

              if (!realRes) {
                _context2.next = 10;
                break;
              }

              page = payload.page, sort = payload.sort;
              _context2.next = 10;
              return put({
                type: 'updateState',
                payload: {
                  queryPagination: {
                    page: page,
                    sort: sort
                  },
                  pagination: createPagination(realRes),
                  dataSource: realRes.content
                }
              });

            case 10:
            case "end":
              return _context2.stop();
          }
        }
      }, cardFetch);
    }),
    // 新建卡片
    cardCreate: /*#__PURE__*/_regeneratorRuntime.mark(function cardCreate(_ref4, _ref5) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function cardCreate$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref4.payload;
              call = _ref5.call;
              _context3.next = 4;
              return call(cardInsert, payload);

            case 4:
              res = _context3.sent;
              return _context3.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context3.stop();
          }
        }
      }, cardCreate);
    }),
    // 更新卡片
    cardUpdate: /*#__PURE__*/_regeneratorRuntime.mark(function cardUpdate(_ref6, _ref7) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function cardUpdate$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              payload = _ref6.payload;
              call = _ref7.call;
              _context4.next = 4;
              return call(_cardUpdate, payload);

            case 4:
              res = _context4.sent;
              return _context4.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context4.stop();
          }
        }
      }, cardUpdate);
    }),
    // 获取卡片详情
    cardDetails: /*#__PURE__*/_regeneratorRuntime.mark(function cardDetails(_ref8, _ref9) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function cardDetails$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              payload = _ref8.payload;
              call = _ref9.call;
              _context5.next = 4;
              return call(_cardDetails, payload);

            case 4:
              res = _context5.sent;
              return _context5.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context5.stop();
          }
        }
      }, cardDetails);
    }),
    // 查询已经分配的租户
    cardTenantFetch: /*#__PURE__*/_regeneratorRuntime.mark(function cardTenantFetch(_ref10, _ref11) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function cardTenantFetch$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              payload = _ref10.payload;
              call = _ref11.call;
              _context6.next = 4;
              return call(cardTenantQueryPage, payload);

            case 4:
              res = _context6.sent;
              return _context6.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context6.stop();
          }
        }
      }, cardTenantFetch);
    }),
    cardTenantAdd: /*#__PURE__*/_regeneratorRuntime.mark(function cardTenantAdd(_ref12, _ref13) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function cardTenantAdd$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              payload = _ref12.payload;
              call = _ref13.call;
              _context7.next = 4;
              return call(cardTenantInsert, payload);

            case 4:
              res = _context7.sent;
              return _context7.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context7.stop();
          }
        }
      }, cardTenantAdd);
    }),
    cardTenantRemove: /*#__PURE__*/_regeneratorRuntime.mark(function cardTenantRemove(_ref14, _ref15) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function cardTenantRemove$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref14.payload;
              call = _ref15.call;
              _context8.next = 4;
              return call(cardTenantDelete, payload);

            case 4:
              res = _context8.sent;
              return _context8.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context8.stop();
          }
        }
      }, cardTenantRemove);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref16) {
      var payload = _ref16.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    }
  }
};